var db_util = require('../../utils/util.js');
Page({

            /**
             * 页面的初始数据
             */
            data: {
                item: {},
                animationData: {},
                show: false,
                placeholder: '快来写下你的评论吧',
                message: '',
                replyItem: {},
                replyList: [],
                userInfo: {}
            },

            /**
             * 生命周期函数--监听页面加载
             */
            onLoad(options) {
                let user = wx.getStorageSync("userInfo");
                this.setData({
                    userInfo: user
                })
                this.getImage(options.id)
                this.getReply(options.id)
            },
            getReply(babayImageId) {
                const that = this
                db_util.getReply(babayImageId, function (res) {
                    that.setData({
                        replyList: res
                    })
                })
            },
            showText() {
                this.setData({
                    show: true,
                    placeholder: '快来写下你的评论吧'
                })
            },
            onClose() {
                this.setData({
                    show: false,
                    replyItem: {},
                    message: ''
                })
            },
            onSubmit() {
                let user = wx.getStorageSync("userInfo");
                if (user == null || user._id == null || user._id == "") {
                    wx.showModal({
                        title: '提示',
                        content: '您还没有登录',
                        showCancel: false,
                        success(res) {
                            if (res.confirm) {
                                wx.redirectTo({
                                    url: '/pages/index/index',
                                })
                            }
                        }
                    })
                    return;
                }
                let reMessage = {}
                reMessage.detailId = this.data.item.id
                reMessage.createTime = db_util.getLocalTime(new Date().getTime());
                reMessage.message = this.data.message
                reMessage.toUserId = this.data.replyItem.userId
                reMessage.toUserName = this.data.replyItem.userName
                reMessage.toMessage = this.data.replyItem.message
                let parentId = ''
                if (this.data.replyItem._id) {
                    parentId = this.data.replyItem._id
                }
                reMessage.parentId = parentId
                reMessage.userId = this.data.userInfo._id
                reMessage.userImage = this.data.userInfo.avatar_url
                reMessage.userName = this.data.userInfo.nick_name
                const that = this
                wx.showLoading()
                db_util.add('mm_babay_message', reMessage, function (_id) {
                    db_util.updateMessageCount(that.data.item.id)
                    let item = that.data.item
                    item.messages += 1
                    wx.hideLoading({
                        success: (res) => {
                            that.setData({
                                show: false,
                                replyItem: {},
                                item: item,
                                placeholder: '快来写下你的评论吧',
                                message: ''
                            })
                            that.getReply(that.data.item.id)
                        },
                    })
                })

            },
            reply(event) {
                let replyItem = event.currentTarget.dataset.item
                console.info(replyItem)
                this.setData({
                    replyItem: replyItem,
                    show: true,
                    placeholder: '回复 @' + replyItem.userName
                })
            },
            getImage(id) {
                wx.showLoading()
                const that = this
                db_util.getImagesById(id, function (res) {
                    let item = {};
                    item.id = res._id
                    item.name = res.nick_name
                    item.avatar = res.avatar_url
                    item.title = res.title
                    item.likeCount = res.likeCount
                    item.messages = res.messages
                    item.userId = res.user_id
                    const images = res.images
                    item.url = images.length > 0 ? images[0].url : "cloud://mengmeng-8swro.6d65-mengmeng-8swro-1302795357/babay_image/215B21g7yS1qvzlu31669127298442.jpeg"
                    item.images = images
                    if (item.images.length <= 0) {
                        item.images.push({
                            url: item.url
                        })
                    }
                    item.createTime = db_util.getLocalTimeForDetailTime(res.create_time)
                    that.setData({
                        item: item
                    })

                    wx.hideLoading({
                        complete: (res) => {},
                    })

                })
            },

            /**
             * 生命周期函数--监听页面初次渲染完成
             */
            onReady() {

            },
            previewImage(e) {
                const current = e.currentTarget.dataset.url
                const images = this.data.item.images
                var imgList = [];
                for (let i = 0; i < images.length; i++) {
                    imgList.push(images[i].url);
                }
                wx.previewImage({
                    current: current, //当前点击的图片链接
                    urls: imgList //图片数组
                })
            },
            heartClick(e) {
                const userId = e.currentTarget.dataset.userid
                const index = e.currentTarget.dataset.index
                let user = wx.getStorageSync("userInfo");
                if (user._id == userId) {
                    wx.showToast({
                        title: '不能给自己点赞～',
                        icon: 'error'
                    })
                    return
                }
                // 顺时针旋转实例
                var animation = wx.createAnimation({
                    duration: 1000,
                    timingFunction: 'ease'
                })
                // 逆时针旋转实例
                var animation1 = wx.createAnimation({
                    duration: 10,
                    timingFunction: 'ease'
                })

                animation.rotate(450).step()
                this.setData({
                    animationData: animation.export()
                })
                setTimeout(function () {
                    animation1.rotate(0).step()
                    this.setData({
                        animationData: animation1.export()
                    })
                }.bind(this), 300);
                const id = this.data.item.id
                //点赞 增加一条点赞记录
                let heart = {}
                heart.babayImageId = id
                heart.openId = user.openId
                heart.createTime = db_util.getLocalTime(new Date().getTime())
                const that = this
                db_util.add('mm_babay_images_hearts', heart, function (_id) {
                    //查询最新点赞数
                    db_util.updateLike(id, function (res) {})
                    //页面直接+1，不在进行查询
                    let item = that.data.item
                    item.likeCount += 1
                    that.setData({
                        item: item
                    })

                })
            },
            /**
             * 生命周期函数--监听页面显示
             */
            onShow() {

            },

            /**
             * 生命周期函数--监听页面隐藏
             */
            onHide() {

            },

            /**
             * 生命周期函数--监听页面卸载
             */
            onUnload() {

            },

            /**
             * 页面相关事件处理函数--监听用户下拉动作
             */
            onPullDownRefresh() {

            },

            /**
             * 页面上拉触底事件的处理函数
             */
            onReachBottom() {

            },

            /**
             * 用户点击右上角分享
             */
            onShareAppMessage() {

            },
            onShareTimeline() {
                return {
                    title: this.data.item.title,
                    query: 'id=' + this.data.item.id, 
                     imageUrl: this.data.item.images[0].url
                    }
                }
            })